package org.opportunity.api.lucene.toolkit.search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class StandardSearcher extends SearcherManager {

    @Override
    public void search(String indexDir, String query) throws IOException, ParseException {
	Directory dir = FSDirectory.open(new File(indexDir));
	IndexReader indexReader = IndexReader.open(dir);
	IndexSearcher is = new IndexSearcher(indexReader);
	QueryParser parser = new QueryParser(Version.LUCENE_36, "contents", new StandardAnalyzer(Version.LUCENE_36));
	Query q = parser.parse(query);
	TopDocs hits = is.search(q, 10);
	for (int i=0;i<hits.scoreDocs.length;i++) {
	    ScoreDoc scoreDoc = hits.scoreDocs[i];
	    Document doc = is.doc(scoreDoc.doc);
	    int docFreq = hits.totalHits;
	    float score = scoreDoc.score;
	    String path = doc.get("path");
	    System.out.println("Hit: " + path + "\n query: " + query + " score: " + score + " docFreq: " + docFreq);

	}
	is.close();

    }

}
